/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen
 * @date: Jun 17, 2003
 * @version: $VERSION$
 */
package edu.psu.geovista.app.pcp.plot.control;

//import edu.psu.geovista.app.pcp.classify.JVisualClassifier;
import edu.psu.geovista.app.pcp.plot.control.tools.BasicTool;
import edu.psu.geovista.app.pcp.plot.gui.BasicPcpUI;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ComponentUI;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Hashtable;

public class DfConfigManager extends JPanel implements WindowListener, ConfigManager{
   public static final boolean DEBUG=false;
    //protected Properties configProps=new Properties(); //define property of each config page
    protected Hashtable configs;
    protected BasicConfig activeConfig;
    protected ComponentUI targetUI;//All the configuration behavior is to change the target UI
    protected Hashtable cmds;
    protected JTabbedPane tabbedPane=new JTabbedPane();
    JFrame popupfrm=new JFrame();// the pop up frame to show config page

    private Settings settings;
    //configure pages
    protected AxisRangeCfg2 axisRangCfg;
    protected AxisLocationCfg  axisLocCfg;
    //protected CategoryCfg ctgCfg;
    protected AnimationCfg animateControl;
    protected AppearanceCfg2 apprCfg;


    public DfConfigManager(ComponentUI targetUI) {
         this.targetUI =targetUI;
        if(targetUI!=null)
         settings = ((BasicPcpUI)targetUI).getSettings();
        else settings=new DfSetting();
        init();
         setupConfig();
    }
    /*******************************************************************************************************
     *                service
     *******************************************************************************************************/

    public void showConfig(String pageName){
        this.setActiveConfig(pageName);
        JFrame myf = this.getPopupfrm();
        if(!myf.isVisible() ){
            //Note: Default close operation is set to HIDE_ON_CLOSE by default.
            //DfConfigManager comp= config;//.getConfig() ;
            myf.addWindowListener(this);
            myf.getContentPane().add(this);
            myf.setBounds(100,100,(int)Math.round(400*1.414),520); // 
            //myf.pack() ;//keep the preference size of child component, it makes setBounds ineffective
            myf.setVisible(true);
        }
        myf.show();

    }

    /*******************************************************************************************************
     *                support
     *******************************************************************************************************/
    protected void init(){
       /*configProps.put(Settings.AXLOC_SHOWX, new Boolean(false));
       configProps.put(Settings.AXLOC_SHOWLB, new Boolean(true));
       configProps.put(Settings.APPR_AUTOFIT, new Boolean(true));
       configProps.put(Settings.APPR_FITV, new Boolean(true));
       configProps.put(Settings.APPR_FITH, new Boolean(false));*/
        //String APP_AUTOFIT="AUTOFIT";//auto fit
    }


    //This is for having one configuring section&button
    private void setupConfig() {
        this.configs =new Hashtable();
        BasicPcpUI ui1 = getTargetUI();
        if(ui1==null)return;
        this.cmds= ui1.getToolsMgr().getCmds();

        setupRangeCfg();
        setupAxisLocationCfg();
        // ESVisualClassifier vc=getTargetUI().getPlot().getVc();

        //ctgCfg=new  CategoryCfg(vc,this); //todo esvc ctgCfg removed
        //this.ctgCfg.addCategoryChangeListener(this);  //todo esvc ctgCfg removed

        //Category Mgr <-

        //Aminate Mgr ->
        setupAnimationCfg();
        //this.add(animateControl,BorderLayout.SOUTH );


        //this.sizeCfg =new SizeCfg(this);
        setupAppearanceCfg();


        setupTabs();
        //tabbedPane.addTab(CFG_SIZE,this.sizeCfg );

        this.setLayout(new BorderLayout());
        this.add(tabbedPane);

        tabbedPane.addChangeListener(new ChangeListener(){
             public void stateChanged(ChangeEvent e) {
                  //Change value on any one of the following page will affect value on the others, need to update
                  //sizeCfg.updateView() ;
                  axisLocCfg.updateView() ;
             }
        });


    }

    protected void setupTabs() {
        tabbedPane.addTab(CFG_RANGE,axisRangCfg);
        tabbedPane.addTab(CFG_LOCATION,axisLocCfg);
        //tabbedPane.addTab(CFG_CLASSIFY,ctgCfg);   //todo esvc ctgCfg removed
        tabbedPane.addTab(CFG_ANIMATION,animateControl);
        tabbedPane.addTab(CFG_APPEARANCE,this.apprCfg );
    }

    protected void setupRangeCfg() {
        this.axisRangCfg = new AxisRangeCfg2(this );
    }

    protected void setupAppearanceCfg() {
        this.apprCfg =new AppearanceCfg2(this);
    }

    protected void setupAnimationCfg() {
        this.animateControl =new AnimationCfg(this);
    }

    protected  void setupAxisLocationCfg() {
        axisLocCfg=new AxisLocationCfg(this);
    }

   /* public void show() {
        this.removeAll() ;
        this.add(this.activeConfig );
    }*/
    public BasicConfig getActiveConfig() {
        return activeConfig;
    }

    public void setActiveConfig(String name) {
        int tabCount = tabbedPane.getTabCount();

        for (int i=0;i<tabCount;i++){
            String title = tabbedPane.getTitleAt(i);
            if(title.equals(name) ){
                tabbedPane.setSelectedIndex(i);
                break;
            }
        }
        /* old version
        this.activeConfig = (BasicConfig)this.configs.get(name) ;
        // 
        //you must explicitly call updateView()
        this.activeConfig.updateView();
        this.toShow() ;*/
    }
    /**
     *
     * @return ComponentUI  for this application, return BasicPcpUI
     */
    public BasicPcpUI getTargetUI() {
        return (BasicPcpUI)targetUI;
    }
    public BasicTool getCmd(String name){
        if(cmds==null){
            BasicPcpUI ui = getTargetUI();

            if(ui!=null)this.cmds= ui.getToolsMgr().getCmds();
        }
        if(cmds!=null)
            return (BasicTool)this.cmds.get(name);
        else
            return null;
    }

    /*******************************************************************************************************
     *                Handle  CategoryCfg
     *******************************************************************************************************/
     /**
     * update upon VisualClassifier changes
     */
    /*private void updateCategoryAxis() {
        String name=this.ctgCfg.getCategoryAxisName() ;
        CategoryList ctgList=ctgCfg.getCtgList() ;
        AxisShowRangeTool cmd=(AxisShowRangeTool) getCmd(BasicTool.AxisShowRangeT );
        cmd.updateCategoryAxis(name,ctgList);

    }*/
   /* public void actionPerformed(ActionEvent e) {
         Object o=e.getSource() ;
        if(o==ctgCfg.getApplyBttn() ){
             updateCategoryAxis();  //todo esvc ctgCfg removed
             this.apply() ;
         }
    }*/
  /*  private void apply() {
        DataSelectTool dst=(DataSelectTool) getCmd(BasicTool.DataSelectT );
        dst.actOnClassify();

    }*/
    /*******************************************************************************************************
     *                Listen to popup frame
     *******************************************************************************************************/
    public void windowOpened(WindowEvent e) {
    }

    public void windowClosing(WindowEvent e) {

    }

    public void windowClosed(WindowEvent e) {

    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
        //todo need redesign initValues() of the Cfgs to make it work.
         updateConfig();
        if(DEBUG)
        System.out.println("Configure Frame actived");
       /* axisRangCfg.updateView() ;
        axisLocCfg.updateView() ;


        sizeCfg.updateView() ;
        apprCfg.updateView() ;*/
    }

    public void windowDeactivated(WindowEvent e) {
    }
    /**
     *  
     */
    private void updateConfig(){
           axisRangCfg.updateView() ;
           axisLocCfg.updateView() ;

    }
    //close the config window
    public void close(){
        JFrame frm = getPopupfrm();
        if (frm!=null){

            frm.setVisible(false);
            frm.dispose();
        }
    }
    /**
     * return sub configurer (cfg), not finished yet
     * @param name
     * @return
     */
    public BasicCfg getCfg(String name){
         if(name.equals(CFG_APPEARANCE) ){
            return this.apprCfg;

        }
        else if(name.equals(CFG_LOCATION) ){
            return this.axisLocCfg ;
        }
        else if(name.equals(CFG_RANGE) ){
            return this.axisRangCfg;

        }
        else return null;
    }

    public JFrame getPopupfrm() {
        return popupfrm;
    }

    public void setPopupfrm(JFrame popupfrm) {
        this.popupfrm = popupfrm;
    }

    public Object getConfigSetting(String name){
       return this.settings.getSetting(name);

    }

    /*public Object getSetting(String name) {
        return settings.getSetting(name);
    }*/
}
